﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Mododor.Ext
{
    public class HttpHelper
    {
        private readonly HttpClient httpClient;
        public HttpHelper(HttpClient httpClient)
        {
            this.httpClient = httpClient;
        }
        public async Task<string> PostJson(string url,string json)
        {
            using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url))
            {
                request.Content = new StringContent(json);
                request.Headers.Host = new Uri(url).Host;
                request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                using (HttpResponseMessage res = await httpClient.SendAsync(request))
                {

                    string jsonString = await res.Content.ReadAsStringAsync();
                    return jsonString;
                }

            }
          

        }

        public async Task<string> GetJson(string url)
        {
            using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url))
            {
                request.Headers.Host = new Uri(url).Host;
                using (HttpResponseMessage res = await httpClient.SendAsync(request))
                {

                    string jsonString = await res.Content.ReadAsStringAsync();
                    return jsonString;
                }

            }
         

        }
    }
}
